﻿using System;
using System.Collections.Generic;

namespace _028.字典的使用
{
	internal class Program
	{
		static void Main(string[] args)
		{
			// 字典的操作
			DictionaryShow();

			// 使用
			Dictionary<int,Item> items = new Dictionary<int,Item>();
			items.Add(1001, new Item(1, "fzn", 100, 20, 10.0f));
		}

		// 字典
		static void DictionaryShow()
		{
			// 字典  键不可重复
			Dictionary<int, string> studentDB = new Dictionary<int, string>();
			studentDB.Add(1, "张三");
			//studentDB.Add(2, "李四");
			studentDB.Add(2, "李四");
			studentDB.Add(3, "王五");
			studentDB.Add(4, "fzn");

			// 判断是否存在
			bool flag = studentDB.ContainsKey(4);
			Console.WriteLine(flag);
			// 一般都会先判断key是否存在
			string value = studentDB[2];
			Console.WriteLine(value);

			foreach (KeyValuePair<int, string> kvp in studentDB)
			{
				Console.WriteLine(kvp.Key + "-" + kvp.Value);
			}
			Console.WriteLine("--------------");
			Dictionary<int, string>.KeyCollection keyCollection = studentDB.Keys;
			foreach (int key in keyCollection)
			{
				Console.WriteLine(key);
			}
			Console.WriteLine("--------------");
			Dictionary<int, string>.ValueCollection valueCollection = studentDB.Values;
			foreach (string values in valueCollection)
			{
				Console.WriteLine(values);
			}
			Console.WriteLine("--------------");
			// 删除  当删除不存在的时候不会报错
			studentDB.Remove(2);
			foreach (KeyValuePair<int, string> kvp in studentDB)
			{
				Console.WriteLine(kvp.Key + "-" + kvp.Value);
			}
		}
	}
}
